﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.Events;
using System.ComponentModel;
using ModulesInfrastructure.ViewModels;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Prism.Regions;
using EventInfrastructure;
using Microsoft.Practices.ServiceLocation;
using ShellInfrastructure;
using ModuleEditPlace.Views;
using ServerConnection.ShopServiceReference;
using ServerConnection;
using ModuleEditPlace.Commands;


namespace ModuleEditPlace.ViewModels
{
    public class EditPlaceViewModel : SharedViewModel, IEditPlaceViewModel, INotifyPropertyChanged
    {
        #region Private members
              
        [Dependency]
        public IRegionManager RegionManager { get; set; }

        private IEventAggregator m_eventAggregator;

        private EditPlaceCommand m_EditPlaceCommand;

        private DeletePlaceCommand m_DeletePlaceCommand;

        /// <summary>
        /// Selected place in listbox
        /// </summary>
        private PlaceAll m_currentPlace;

        /// <summary>
        /// address of current (selected) language
        /// </summary>
        private string m_addressOfCurrentLanguage;             

        /// <summary>
        /// localization list of selected place
        /// </summary>
        private List<PlaceLocalizationModel> m_currentLocalizationList;

        /// <summary>
        /// Selected localization (address - language pair) from combobox
        /// </summary>
        private PlaceLocalizationModel m_currentLocalization;

        private void onPlaceSelectEvent(string placeName)
        {
            RegionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
            IRegion region = RegionManager.Regions[RegionNames.RightRegionName];
            region.Activate(ServiceLocator.Current.GetInstance<IEditPlaceView>());

            ///update current place
            CurrentPlace = Service.Instance.GetPlaceAllByName(placeName);

            ///Get localization list from current (selected) place
            if(m_currentPlace.Localizations != null)
            LocalizationsOfCurrentPlace = m_currentPlace.Localizations.ToList();

            //this.ShowProduct = new List<ProductFull>();
            //this.ShowProduct.Add(Service.Instance.GetProductFullById(id, "eng"));
            //RaisePropertyChanged(() => this.ShowProduct);
        }

        /// <summary>
        /// This method is called up when the property is changed
        /// </summary>
        /// <param name="propertyName"></param>
        private void m_OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                //fire event
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion Private members


        #region Constructor
         /// <summary>
        /// Constructor
        /// </summary>       
        public EditPlaceViewModel(IEventAggregator eventAggregator)
        {            
            ///Set the eventAggregator
            this.m_eventAggregator = eventAggregator;

            ///Create commands, pass the viewmodel
            m_EditPlaceCommand = new EditPlaceCommand(this);
            m_DeletePlaceCommand = new DeletePlaceCommand(this);

            m_eventAggregator.GetEvent<PlaceSelectEvent>().Subscribe(onPlaceSelectEvent);
        }
        
        #endregion Constructor


        #region Public properties

        public EditPlaceCommand EditPlaceCommand
        {
            get
            {
                return m_EditPlaceCommand;
            }             
        }


        public DeletePlaceCommand DeletePlaceCommand
        {
            get
            {
                return m_DeletePlaceCommand;
            }
        }


        /// <summary>
        /// Sets the m_currentPlace, when Place is selected in the ListBox
        /// </summary>
        public PlaceAll CurrentPlace
        {
            get
            {
                return m_currentPlace;  
            }
            set
            {
                m_currentPlace = value;
                m_OnPropertyChanged("CurrentPlace");
            }
        }       


        /// <summary>
        /// For binding to combobox
        /// Sets the m_currentLocalizationList, when Place is selected in the ListBox
        /// </summary>
        public List<PlaceLocalizationModel> LocalizationsOfCurrentPlace
        {
            get
            {
                return m_currentLocalizationList;
            }
            set
            {
                m_currentLocalizationList = value;
                m_OnPropertyChanged("LocalizationsOfCurrentPlace");
            }
        }

        /// <summary>
        /// Selected localization (address - language pair) in combobox
        /// combobox keeps list, but when the item is selected, combobox gives us one object
        /// </summary>
        public PlaceLocalizationModel AddressOfCurrentLanguage
        {
            get
            {
                return m_currentLocalization;
            }
            set
            {
                m_currentLocalization = value;

                if (m_currentLocalization != null)
                {
                    ///Update textBox with address
                    StringAddressOfCurrentLanguage = m_currentLocalization.Address;
                    m_OnPropertyChanged("AddressOfCurrentLanguage");
                }
            }
        }

        /// <summary>
        /// when choose language from combobox or type a new address in the textbox
        /// </summary>
        public string  StringAddressOfCurrentLanguage
        {
            get
            {
                return m_addressOfCurrentLanguage;
            }
            set
            {
                m_addressOfCurrentLanguage = value;
                m_OnPropertyChanged("StringAddressOfCurrentLanguage");
            }
        }

        #endregion Public properties


        #region Public methods

        /// <summary>
        /// Delete selected place
        /// </summary>
        public void DeletePlace()
        {
            ///Delete current place    
            Service.Instance.DeletePlace(CurrentPlace.Id);

            ///Update places ListBox in the Left region
            ///1 - not used
            m_eventAggregator.GetEvent<AddOrUpdatePlaceEvent>().Publish(1);
        }

        /// <summary>
        /// Update selected place
        /// </summary>
        public void UpdatePlace()
        {
            ///Send current selected updated place to server
            Service.Instance.UpdatePlace(CurrentPlace);

            ///Update places ListBox in the Left region
            ///1 - not used
            m_eventAggregator.GetEvent<AddOrUpdatePlaceEvent>().Publish(1);
        }

        #endregion


        #region Events

        /// <summary>
        /// PropertyChanged is needed for binding to ui
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion Events

    }//class
}
